<template>
	<view>
		<view class="logo">
			<image src="/static/icon/logo.png"></image>
			<view class="ad">上网买好房 就上房联网！</view>
		</view>
		
		<view class="login-btn">
			<u-button type="primary" shape="circle" @click="getUserInfo">一键登录</u-button>
			<view class="tips">登录即表明同意<text @click="$app.openXY(5)">《用户协议》</text><text @click="$app.openXY(3)">《隐私政策》</text></view>
		</view>
	</view>
</template>

<script>
	import socket from '@/common/socket';
	export default {
		data() {
			return {
				tokenTemp:''
			}
		},
		methods: {
			getUserInfo(){
				uni.getUserProfile({
					desc:'完善会员信息',
					success: (res) => {
						console.log(res.userInfo)
						console.log(this.tokenTemp)
						this.$u.post('/api/user/profile',{
							avatar:res.userInfo.avatarUrl,
							nickname:res.userInfo.nickName
						},{
							token:this.tokenTemp //临时token
						}).then(res=>{
							this.logined(this.tokenTemp)
							
						})
					}
				})
			},
			logined(token){
				this.$u.vuex('token',token)
				this.$store.dispatch('getUserInfo',this)
				// 登录后重新连接
				socket.init()
				let pages = getCurrentPages()
				if (pages.length >= 2) {
					let beforePage = pages[pages.length - 2]
					uni.navigateBack({
						success:()=>{
							beforePage.onLoad(beforePage.options)
						}
					})
				} else {
					this.$app.jump('/pages/index/index',!0)
				}
			}
		},
		onLoad() {
			uni.login({
				provider:'weixin',
				success: (res) => {
					this.$u.get('/api/user/xcxLogin',{code:res.code}).then(res=>{
						this.tokenTemp = res.token
						// 设置临时token
						// 存在头像即为已注册用户，可以直接登录
						res.avatar && this.logined(res.token)
					})
				}
			})
		}
	}
</script>

<style lang="scss">
	.logo{padding-top: 10vh;text-align: center;
		image{width: 300rpx;height: 248rpx;}
		.ad{color: #000;font-size: 32rpx;}
	}
	.login-btn{padding: 0 40rpx;margin-top: 30vh;
		.tips{text-align: center;font-size: 26rpx;color: #666;margin-top: 33rpx;
			text{color: #1f87f9;}
		}
	}
</style>
